//index.js
//获取应用实例
import {
  getCoupon
} from '../../api/coupon.js'
const app = getApp()
Page({
  data: {
    statusType: ["待领取", "已领取"],
    canCoupons: [],
    cantCoupons: [],
    busid: 0,
    currentType: 0,
    hasNoCoupons: true,
    little: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
    ids: '',
    orderType: '',
    couponId: ''
  },
  onShow() {
    if (!wx.getStorageSync('access_token')) {
      wx.showModal({
        title: '请先登录',
        content: '',
        showCancel: false,
        success: (res) => {
          wx.navigateBack({
            delta: 1 //返回上一级页面
          })
        }
      })
      return
    }

  },
  statusTap(e) {
    this.setData({
      currentType: e.currentTarget.dataset.index
    })
  },
  listenerCouponsInput: function (e) {
    this.data.coupons = e.detail.value;
    this.data.id = e.currentTarget.dataset.id;
  },
  listenerDuiHuan: function () {
    wx.request({
      url: app.globalData.urls + '/discounts/fetch',
      data: {
        // id: this.data.busid,
        pwd: this.data.coupons,
        token: app.globalData.token
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          wx.showToast({
            title: '礼券兑换成功',
            icon: 'success',
            duration: 2000
          })
        }
        if (res.data.code == 20001 || res.data.code == 20002) {
          wx.showModal({
            title: '兑换失败',
            content: '礼券已经兑换完了',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 20003) {
          wx.showModal({
            title: '兑换失败',
            content: '兑换数量已达最大上限',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 20000) {
          wx.showModal({
            title: '兑换失败',
            content: '输入的口令有误，请重新输入',
            showCancel: false
          })
          return;
        }
      }
    })
  },
  goDrawCoupon() {
    this.setData({
      coupons: [],
      isFinish: false,
      index: 1,
    })
    wx.redirectTo({
      url: './drawCoupon',
    })
  },
  async onLoad(e) {
    var that = this
    if (app.globalData.iphone == true) {
      that.setData({
        iphone: 'iphone'
      })
    }
    e.canCoupons = JSON.parse(e.canCoupons)
    e.cantCoupons = JSON.parse(e.cantCoupons)
    console.log(e.canCoupons, e.cantCoupons)
    this.setData({
      couponId: e.couponId
    })
    e.canCoupons.map(item => {
      if (item.id == e.couponId) {
        item.beforeStatus = true
      } else {
        item.beforeStatus = false
      }
    })
    this.setData({
      canCoupons: e.canCoupons.map(item => {
        item.endTime = item.endTime.substring(0, 10)
        return item
      }),
      cantCoupons: e.cantCoupons.map(item => {
        item.endTime = item.endTime.substring(0, 10)
        return item
      }),
      orderType: e.orderType
    })

    wx.hideShareMenu()
  },
  showDesc(e) {
    wx.showModal({
      content: e.currentTarget.dataset.desc,
      showCancel: false
    })
  },

  async select(e) {
    console.log(e.currentTarget.dataset.item);
    var item = e.currentTarget.dataset.item
    if (item.IsUsed == 1) {
      return;
    }
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]
    if (item.id == this.data.couponId) {
      prevPage.setData({
        coupon: null,
        // allGoodsAndYunPrice: (prevPage.data.allGoodsAndYunPrice - item.price).toFixed(2)
      })
    } else {
      prevPage.setData({
        coupon: item
      })
    }
    wx.navigateBack({
      item
    })
  },
  goPay() {
    setTimeout(function () {
      wx.switchTab({
        url: '/pages/cloudPay/cloudPay',
        complete: res => {
          console.log(res)
        }
      })
    }, 200)
  },
  async getCoupons(ids) {
    var that = this
    let res = await getCoupon('NEW', 1, wx.getStorageSync('access_token'))
    console.log(res)
    if (res && res.code == 10000) {
      this.setData({
        coupons: res.data.records.map(item => {
          item.endTime = item.endTime.substring(0, 10)
          return item
        })
      })
    }
    // let url = 'coupon/get_coupons?CustId=' + app.globalData.uid
    // if (ids) {
    //   url = 'coupon/get_coupons?CustId=' + app.globalData.uid + '&GoodsIds=' + ids;
    // }

    // console.log()
    // app.get(url, res => {
    //   if (res && res.code == "0") {
    //     console.log(res.result);
    //     that.setData({
    //       // hasNoCoupons: false,
    //       coupons: res.result.map(item => {
    //         item.isExpried = (Date.parse(item.EndTime)) < (new Date()).getTime()
    //         item.EndTime = item.EndTime.substring(5, 10).replace("-", ".")
    //         item.CreateTime = item.CreateTime.substring(5, 10).replace("-", ".")
    //         item.Val = item.Val.substring(0, item.Val.indexOf("."))
    //         return item;
    //       })
    //     });
    //   }
    // });
  },

})